package modulesAPI.v2.composite;
import java.util.GregorianCalendar;

import person.Employee.Role;
import person.Person;

import modulesAPI.v2.Module;
import modulesAPI.v2.ViewType;
import modulesAPI.v2.basics.ModCalendar;
import modulesAPI.v2.basics.ModComboBox;
import modulesAPI.v2.basics.ModDoubleTextField;

import modulesAPI.v2.basics.ModTextArea;
import com.caterina.components.CPanel;
import com.caterina.resources.CModules;
import com.caterina.resources.CWords;

import contract.EmployeeContract;


public class MEmployeeContract extends Module<EmployeeContract>{
	/**
	 * Crea un nuovo modulo per l'inserimento/rappresentazione di un dipendente
	 * */
	public MEmployeeContract(Module<?> ownerModule, EmployeeContract content, String tag, String description, ViewType viewType,int...options) {
		super(ownerModule, content, tag, description, viewType,options);
		if(this.getContent().isTemporaryContract())this.contractType=ContractType.TEMPORARY;
		else this.contractType=ContractType.NON_TEMPORARY;
		createModules();
		this.addModules(person, role, temporary, stipulationDate, expiration,wages, clauses);
	}
	/**
	 * Crea un nuovo modulo per l'inserimento/rappresentazione di un dipendente
	 * */
	public MEmployeeContract(Module<?> ownerModule, String tag, String description, ViewType viewType) {
		this(ownerModule, new EmployeeContract(new GregorianCalendar(), new GregorianCalendar(), "", Role.OTHER_EMPLOYEE, new Person("", "", "", "", "", "", ""),0, true),
				tag, description, viewType);
	}
	
	private void createModules(){
		person = new MPerson(this, this.getContent().getPerson(), "RECRUITED", "RECRUITED_DESC", this.getViewType());
		role = new ModComboBox(this, this.getContent().getRole(), "RECRUITED_ROLE", "RECRUITED_ROLE_DESC", this.getViewType(), Role.values());
		temporary=new ModComboBox(this, this.contractType, "CONTRACT_TYPE", "CONTRACT_TYPE_DESC", this.getViewType(), ContractType.values());
		expiration = new ModCalendar(this, this.getContent().getValidityDate(), "CONTRACT_VALIDITY_DATE", "CONTRACT_VALIDITY_DATE_DESC", this.getViewType());
		stipulationDate = new ModCalendar(this, this.getContent().getStipulationDate(), "CONTRACT_STIPULATION_DATE", "CONTRACT_STIPULATION_DATE_DESC", this.getViewType());
		wages = new ModDoubleTextField(this,this.getContent().getWages(), "CONTRACT_WAGES", "CONTRACT_WAGES_DESC", this.getViewType());
		clauses = new ModTextArea(this, this.getContent().getClauses(), "CONTRACT_CLAUSES", "CONTRACT_CLAUSES_DESC", this.getViewType());
		if (!this.getContent().isTemporaryContract()) this.expiration.setVisible(false);
	}
	@Override
	protected CPanel getReadOnlyPanel(boolean isSelected) {
		return this.getDefaultPanel();
	}

	@Override
	protected CPanel getWritablePanel(boolean isSelected) {
		return this.getDefaultPanel();
	}

	@Override
	public CPanel getInListPanel(boolean isSelected) {
		return CModules.formatForList(this.getContent().getPerson().getName()+"  "+this.getContent().getPerson().getSurname(), 
				CWords.getWord(this.getContent().getRole().name())+" "+(this.getContent().isTemporaryContract()?CWords.getWord("TEMPORARY_CONTRACT"):CWords.getWord("INDEFINITE_PERIOD")), isSelected);
	}
	@Override
	protected void beforeFireChange(Module<?> mod) {
		this.refreshContent();
		if(mod.getTag(false).equalsIgnoreCase("CONTRACT_TYPE")){
			if(temporary.getContent().name().equalsIgnoreCase("NON_TEMPORARY")){
				this.expiration.setVisible(false);
			}

			else 
				this.expiration.setVisible(true);
		}
	}
	private void refreshContent(){
		boolean temporaryVal=false;
		if(temporary.getContent().name().equalsIgnoreCase("TEMPORARY")) 
			temporaryVal=true;
		this.setContent(new EmployeeContract(
				this.getContent().getId(), 
				stipulationDate.getContent(),
				expiration.getContent(),
				clauses.getContent(),
				(Role)role.getContent(),
				person.getContent(),
				wages.getContent(),
				temporaryVal
		));
	}
	@Override
	protected void afterFireChange(Module<?> mod) {}
	@Override
	protected CPanel getOneLinePanel(boolean isSelected) {
		return null;
	}
	private MPerson person;
	private ModDoubleTextField wages;
	private ModCalendar stipulationDate;
	private ModCalendar expiration;
	private ModTextArea clauses;
	private ModComboBox role;
	private ModComboBox temporary;
	private ContractType contractType;
	public static final int INVISIBLE_ROLE=0;
	private enum ContractType{TEMPORARY,NON_TEMPORARY}
}